package com.frameAnimation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_img;
    private ImageView iv_sea;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindViewById();
        animImg();
        ClickEvent();
    }

    //实现缩放动画
    private void scaleAnimation() {
        ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim);

//        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
//        scaleAnimation.setDuration(3000);

        iv_sea.startAnimation(scaleAnimation);
    }

    private void ClickEvent() {
        findViewById(R.id.btn_alpha).setOnClickListener(v -> alphaAnimation());
        findViewById(R.id.btn_scale).setOnClickListener(v -> scaleAnimation());
        findViewById(R.id.btn_rotate).setOnClickListener(v -> rotateAnimation());
        findViewById(R.id.btn_translate).setOnClickListener(v -> translateAnimation());
        findViewById(R.id.btn_animSet).setOnClickListener(v -> animationSet());
    }

    //动画效果集
    private void animationSet() {
        AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(MainActivity.this, R.anim.set_anim);

//        AnimationSet animationSet = new AnimationSet(true);
//        AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f);
//        ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f);
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
//        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 200, 100);
//        animationSet.addAnimation(alphaAnim);
//        animationSet.addAnimation(scaleAnimation);
//        animationSet.addAnimation(translateAnimation);
//        animationSet.addAnimation(rotateAnimation);
//        animationSet.setDuration(5000);

        iv_sea.startAnimation(animationSet);
    }

    //平移动画
    private void translateAnimation() {
        TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_anim);

//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 300, 0, 300);
//        translateAnimation.setDuration(3000);

        iv_sea.startAnimation(translateAnimation);
    }

    //旋转动画
    private void rotateAnimation() {
        RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_anim);

//        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 320, 320);
//        rotateAnimation.setDuration(3000);

        iv_sea.startAnimation(rotateAnimation);
    }

    //实现渐变动画
    private void alphaAnimation() {
        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_anim);

//        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
//        alphaAnim.setStartOffset(1000);// 动画延迟开始时间（ms）
//        alphaAnim.setDuration(3000);//动画持续时间（ms），必须设置，动画才有效果
//        alphaAnim.setRepeatCount(3);//动画重复次数,Animation.INFINITE为无限循环播放
//        alphaAnim.setFillAfter(true);//动画播放完后，视图是否会停留在动画结束的状态，优先于fillBefore值，默认为false
//        alphaAnim.setRepeatMode(Animation.REVERSE);//选择重复播放动画模式，restart代表正序重放，reverse代表倒序回放，默认为restart

        iv_sea.startAnimation(alphaAnimation);
    }

    private void animImg() {
        iv_img.setImageResource(R.drawable.wifi);
        AnimationDrawable animationDrawable = (AnimationDrawable) iv_img.getDrawable();
        animationDrawable.start();
    }

    private void bindViewById() {
        iv_img = findViewById(R.id.iv_img);
        iv_sea = findViewById(R.id.iv_sea);
    }


}
